package com.SparkCore.RDD.Operator.Transform

import org.apache.spark.{SparkConf, SparkContext}


/**
 * 获取分区索引
 */
object Spark03_RDD_Operator_Transform {

  def main(args: Array[String]): Unit = {

    //创建环境
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("RDD")
    val sparkContext = new SparkContext(sparkConf)

    //创建RDD算子 -- map
    val rdd = sparkContext.makeRDD(List(1,2,3,4) , 2)

    val mpiRDD = rdd.mapPartitionsWithIndex(
      (index, iter) => {
        //获取第二个分区里面的数据
        if (index == 1) {
          iter
        } else {
          Nil.iterator  //空的迭代器
        }
      }
    )

    mpiRDD.collect().foreach(println )

    //关闭环境
    sparkContext.stop()
  }
}
